import MyPromise from 'components/Promise';

const p1 = () => {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('this is p1'); // 3: this is p1
      resolve('p1');
    }, 3000);
  });
};
const p2 = () => {
  return new Promise((resolve) => {
    console.log('this is p2'); // 1: this is p2
    resolve('p2');
  });
};

p2()
  // .then((res) => {
  //   console.log('then 1', res);
  //   return Promise.resolve(123);
  // })
  // NOTE finally函数只向下传递了上一个then的返回值 并不会被自己返回的Promise对象覆盖
  .finally((res) => {
    console.log('finally:', res); // 2: finally: undefined
    // return Promise.resolve(456)
    return p1();
  })
  .then(
    (res) => {
      console.log('success:', res); // 4: success: p2
    },
    (err) => {
      console.log('error:', err);
    }
  );
